package com.android.dblocation

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.camera.base.bean.YLLocation
import com.camera.routemodule.routeinterface.BDLocationListener
import com.tbruyelle.rxpermissions.RxPermissions
import rx.functions.Action1

class DBLocationCommonBinder: DBLocationBinderImpl {
    var context: Context? = null
    // 定位相关
    private var mLocClient: LocationClient? = null
    private val myListener = MyLocationListener()
    private val binderBDLocationListeners: MutableSet<BDLocationListener> = mutableSetOf()
    var mYLLocation = YLLocation(0.0,0.0);
    override fun init(context: Context?) {
        this.context = context
    }

    override fun startLocation() {
        var permissions = arrayOf<String?>(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
        RxPermissions.getInstance(context)
            .request(*permissions)
            .subscribe(object : Action1<Boolean?> {
                override fun call(t: Boolean?) {
                    if (t == true){
                        // 定位初始化
                        try {
                            LocationClient.setAgreePrivacy(true)
                            mLocClient = LocationClient(context)
                        } catch (e: Exception) {
                            e.printStackTrace()
                            Log.e("TAG", "initLocation: "+e.toString() )
                        }
                        mLocClient?.registerLocationListener(myListener)
                        val option = LocationClientOption()
                        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
                        // 打开gps
                        option.isOpenGps = true
                        // 设置坐标类型
                        option.setCoorType("bd09ll")
                        option.setScanSpan(1000)
                        mLocClient?.setLocOption(option)
                        mLocClient?.start()
                    }

                }
            })
    }

    override fun stopLocation() {
        mLocClient?.stop()
    }

    override fun addLocationListener(listener: BDLocationListener) {
        binderBDLocationListeners.add(listener)
        binderBDLocationListeners.forEach {
            it.onLocationChange(mYLLocation)
        }
    }

    override fun removeLocationListener(listener: BDLocationListener) {
        binderBDLocationListeners.remove(listener)
    }

    override fun getBDLocationListenerSize(): Int {
        return binderBDLocationListeners.size
    }

    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            if (location.locType == BDLocation.TypeGpsLocation
                || location.locType == BDLocation.TypeNetWorkLocation
                || location.locType == BDLocation.TypeOffLineLocation
                || location.locType == BDLocation.TypeGnssLocation
                || location.locType == BDLocation.TypeCoarseLocation
            ) {
                mYLLocation = YLLocation(location.latitude,location.longitude)
                binderBDLocationListeners.forEach {
                    it.onLocationChange(mYLLocation)
                }
            }


        }
    }
}